﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using FGUFW.Core.System;
using UnityEngine.AddressableAssets;

namespace FGUFW.Play.StackedTower
{
    public class StackedTowerManager : PlayManager<StackedTowerManager>
    {
        static public class Config
        {
            public const string MAX_LEVEL_KEY = "StackedTowerManager.MAX_LEVEL_KEY";
        }

        public GameObject EndPanel;
        public GameObject StartPanel;
        public GameObject CubeCopy;
        public Text LevelText;
        public Text MaxLevelText;
        public int Level=0;

        GameObject _currectCube;

        // Start is called before the first frame update
        void Start()
        {
            
        }

        // Update is called once per frame
        void Update()
        {
            
        }

        public void GameEnd()
        {
            if(EndPanel.activeSelf)
            {
                return;
            }
            EndPanel.SetActive(true);
            LevelText.text = "层数:"+ Level;
            MaxLevelText.text = "最高纪录:"+ PlayerPrefs.GetInt(Config.MAX_LEVEL_KEY,0);
            if(PlayerPrefs.GetInt(Config.MAX_LEVEL_KEY,0)<Level)
            {
                PlayerPrefs.SetInt(Config.MAX_LEVEL_KEY,Level);
            }
        }

        public void OnClickQuit()
        {
            Addressables.LoadSceneAsync("GameLobby");
        }

        public void OnClickStart()
        {
            StartPanel.SetActive(false);
            OnClickAdd();
        }

        public void OnClickAdd()
        {
            if(_currectCube)
            {
                _currectCube.GetComponent<Cube>().enabled=false;
                _currectCube.AddComponent<Rigidbody>();
            }
            Level++;
            var cube = Instantiate(CubeCopy).GetComponent<Cube>();
            cube.Level=Level;
            cube.gameObject.SetActive(true);
            _currectCube = cube.gameObject;
        }
    }
}
